﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using AzureHelperWCFTester.ServiceReference1;
using System.Net;
using System.IO;

namespace AzureHelperWCFTester
{
  class Program
  {
    internal class Contact
    {
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string Phone { get; set; }
    }

    public static string Post(string url, string data)
    {


      string vystup = null;
      try
      {
        //Our postvars
        byte[] buffer = Encoding.ASCII.GetBytes(data);
        //Initialisation, we use localhost, change if appliable
        HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url);
        //Our method is post, otherwise the buffer (postvars) would be useless
        WebReq.Method = "POST";
        //We use form contentType, for the postvars.
        WebReq.ContentType = "text/html";
        //The length of the buffer (postvars) is used as contentlength.
        WebReq.Headers.Add("clientID:12345");
        WebReq.Headers.Add("response:success");
        WebReq.ContentLength = buffer.Length;
        //We open a stream for writing the postvars
        Stream PostData = WebReq.GetRequestStream();
        //Now we write, and afterwards, we close. Closing is always important!
        PostData.Write(buffer, 0, buffer.Length);
        PostData.Close();
        //Get the response handle, we have no true response yet!
        HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
        //Let's show some information about the response
        Console.WriteLine(WebResp.StatusCode);
        Console.WriteLine(WebResp.Server);

        //Now, we read the response (the string), and output it.
        Stream Answer = WebResp.GetResponseStream();
        StreamReader _Answer = new StreamReader(Answer);
        vystup = _Answer.ReadToEnd();

        //Congratulations, you just requested your first POST page, you
        //can now start logging into most login forms, with your application
        //Or other examples.
      }
      catch (Exception ex)
      {
        Console.WriteLine(ex.Message);
      }
      return vystup.Trim() + "\n";

    }

    static void Main(string[] args)
    {
      //ServiceReference1.HelperServiceClient client = new ServiceReference1.HelperServiceClient();
      Console.WriteLine(Post("http://localhost:51397/API/GetCommand", "clientID=12345&response=success"));
      //Console.WriteLine(Post("http://remotify.cloudapp.net/API/PostResponse", "clientID=12345&response=success"));
      
      ////Test WCF client for activity
      //Console.WriteLine(client.GetMyName("Anshuman"));

      ////Test create queue
      //Console.WriteLine(client.CreateQueue("requestqueue").ToString());

      //while (true)
      //{
      //  //Test put message in queue
      //  Console.WriteLine(client.SendMessage("1001", "requestqueue", "print").ToString());
      //  //Console.WriteLine(client.SendMessage("1", "responsequeue", "hello world").ToString());
      //  //Console.WriteLine(client.GetMessage("101201", "requestqueue", true));
      //  Console.WriteLine(client.GetMessage("1001", "requestqueue", true));
      //  //Console.WriteLine(client.GetMessage("*", "responsequeue", true));
      //  //Console.WriteLine(client.GetMessage("*", "requestqueue", true));
      //  //Console.WriteLine(client.GetQueueDepth("requestqueue").ToString());
      //  Console.WriteLine(client.GetQueueDepth("responsequeue").ToString());
      //  System.Threading.Thread.Sleep(9);
      //}

      ////Test queue depth
      //Console.WriteLine(client.GetQueueDepth("requestqueue").ToString());

      ////Test queue get message
      //Console.WriteLine(client.GetMessage("requestqueue", false).ToString());

      //DataSet set = client.GetQueueList();
      //Console.WriteLine(set.GetXml());

      //string tableName = "WP7DeviceTable";
      //string partitionKey = "Devices";

      ////Console.WriteLine(client.DeleteTable(tableName));
      //Console.WriteLine(client.CreateTable(tableName));

      //DeviceInfo device = new DeviceInfo();
      //device.DeviceID = "1";
      //device.DeviceName = "WP7";
      //Console.WriteLine(client.InsertRow("WP7DeviceTable", partitionKey, device.DeviceID, device));

      //device.DeviceID = "1001";
      //device.DeviceName = "LinuxClient1";
      //Console.WriteLine(client.InsertRow("WP7DeviceTable", partitionKey, device.DeviceID, device));

      //device.DeviceID = "1002";
      //device.DeviceName = "LinuxClient2";
      //Console.WriteLine(client.InsertRow("WP7DeviceTable", partitionKey, device.DeviceID, device));

      //device.DeviceID = "2001";
      //device.DeviceName = "WindowsClient1";
      //Console.WriteLine(client.InsertRow("WP7DeviceTable", partitionKey, device.DeviceID, device));

      //device.DeviceID = "2002";
      //device.DeviceName = "WindowsClient2";
      //Console.WriteLine(client.InsertRow("WP7DeviceTable", partitionKey, device.DeviceID, device));
      ////Console.WriteLine(client.CreateTable(tableName).ToString());

      //string[] list = client.GetTablesList();
      //foreach (string name in list)
      //{
      //  Console.WriteLine(name);
      //}

      // Insert a row
      //Dictionary<string, string> dict = client.DisplayTable(tableName);

      //// insert another row
      //c.FirstName = "Jane";
      //tables.InsertRow(TableName, PartitionKey, c.FirstName + c.LastName, c);
      //Console.WriteLine("Row Inserted\n");

      //// insert another row
      //c.LastName = "Smith";
      //tables.InsertRow(TableName, PartitionKey, c.FirstName + c.LastName, c);
      //Console.WriteLine("Row Inserted\n");

      //Console.WriteLine(client.DeleteTable("WP7DeviceTable").ToString());


      //Contact c = new Contact();
      //c.FirstName = "John";
      //c.LastName = "Doe";
      //c.Phone = "123-123-1234";

      //using (var webClient = new WebClient())
      //{
      //  webClient.Headers.Add("clientID:12345");
      //  webClient.Headers.Add("response:success");
      //  var dataToPost = Encoding.Default.GetBytes("clientID=12345&response=success");
      //  var result = webClient.UploadData("http://localhost:51397/API/PostRespose", "POST", dataToPost);
      //  // do something with the result
      //}

      #region TestCommands
      //CommTranslate[] commands = client.GetCommands("1001");
      //foreach (CommTranslate command in commands)
      //{
      //  Console.WriteLine(command.TagOfCommand);
      //  Console.WriteLine(command.ActualComm);
      //}
      #endregion

      #region SetUpCommands
      //string commandsTableName = "WP7CommandsTable";
      //string partitionKey = "Commands";
      ////Console.WriteLine(client.DeleteTable(commandsTableName));
      //Console.WriteLine(client.CreateTable(commandsTableName));

      //CommTranslate command = new CommTranslate();
      //command.CommandID = "1";
      //command.DeviceID = "1001";
      //command.TagOfCommand = "shutdown";
      //command.ActualComm = "shutdown -h";
      //Console.WriteLine(client.CommandsTable_InsertRow(commandsTableName, partitionKey, command.CommandID, command));

      //command.CommandID = "2";
      //command.DeviceID = "1001";
      //command.TagOfCommand = "print";
      //command.ActualComm = "lpr";
      //Console.WriteLine(client.CommandsTable_InsertRow(commandsTableName, partitionKey, command.CommandID, command));

      #endregion
    }
  }
}